package org.baicaixiaozhan.learncommon.util.accesslimit;

import org.baicaixiaozhan.learncommon.exception.UnsupportedOperationException;
import org.baicaixiaozhan.learncommon.util.PreconditionUtils;
import org.baicaixiaozhan.learncommon.util.SpringContextHandler;
import org.redisson.api.RRateLimiter;
import org.springframework.data.redis.core.RedisTemplate;


/**
 * DESC:
 *
 * @since 2021-07-03
 * @author baicaixiaozhan
 */
public class AccessWithRedisRateLimiter implements AccessLimiter {

    @Override
    public void startAccessLimit() {
        RedisTemplate redisTemplate = SpringContextHandler.getBean("redisTemplate", RedisTemplate.class);
        RRateLimiter defaultRedissonRateLimiter = SpringContextHandler.getBean("defaultRedissonRateLimiter", RRateLimiter.class);
        PreconditionUtils.checkNotNull(redisTemplate, UnsupportedOperationException::new);
        defaultRedissonRateLimiter.acquire(1);
    }
}
